var CDR = CDR ||{};
/**
 * 话单稽核- 清单统计总量
 */
CDR.countTotal = Ext.extend(Ext.Panel,{
	closable:true
	,width:'100%'
	,initComponent:function(){
		this.tbar = this.buildBbar(this);
		var gp = new CDR.countTotal.gridPanel();
		this.items = [new QueryStatcdrCounTotalForm({
					thisGrid:gp
				}),gp] ;
		this.formPanel = new CDR.countTotal.searchPanel({thisGrid:gp});
		this.updatePanel =  null ;
		CDR.countTotal.superclass.initComponent.call(this);
	}
	,buildBbar: function(mp) {
   		return [
   			{
   				text : "显示查询",
   				iconCls: 'ico-dot-search-open',
   				handler:function(btn){
   					var m = btn.ownerCt.ownerCt;
   					var g = m.items.get(1);
   					var a = m.items.get(0);
   					a.setHeight(70);
   					a.searchBtn = btn;
   					if(a.collapsed) {
   						var h1 = m.getHeight()-m.tbar.dom.clientHeight-a.getHeight();
   						a.expand(true);
   						g.setHeight(h1-70);
   						btn.setText("隐藏查询");
   						btn.setIconClass("ico-dot-search-hidden");
   					} else {
   						a.collapse(true);
   						g.setHeight(m.getHeight()-m.tbar.dom.clientHeight-1);
   						btn.setText("显示查询");
   						btn.setIconClass("ico-dot-search-open");
   					}
   				}
   			},'-',{
   				text : "问题处理",
   				handler :this.updateConntTotalData,
   				iconCls: 'ico-dot-edit-news'
   			}
//   			,{
//   				text: "按条件查询",
//   				handler:function(btn){
//   					var tform = btn.ownerCt.ownerCt.formPanel;
//   	    			tform.show();
//   				},
//    			iconCls: 'ico-dot-search'
//	   		}
//   			,{xtype:'spacer',width:30}
   			,{
   				text: "数据导出",
   				handler:this.exportData,
    			iconCls: 'ico-dot-report'
	   		}
	   		,'->',{
                    	text:'使用说明'					
                    	,iconCls:'ico-dot-book-question'
                    	,handler:function(){
                    		SM.Explanation.showExplanation(45);
                    	}
                    }
	   		] ;
   	}
	,buildGrid: function(){
		
		return[{
			xtype:'grid'
			,viewConfig:{
				forceFit:true
			}
			
		}];
	}
	,updateConntTotalData : function(btn) {
   		 var mPanel = btn.ownerCt.ownerCt;
   		 var gp = mPanel.items.get(1) ;
   		 var rows= gp.getSelectionModel().getSelections(); 
   		 if(rows.length==0) {
 			MsgTip.msg({
			 	msg:"请选择一条记录"
			 	,cmp: mPanel
			 	,pos:"top"
			 	,time:2
			});
   			 return ;
   		 }
   		 if(!mPanel.updatePanel) {
   			mPanel.updatePanel = new Ext.Window({
	   			layout:"fit",
	   			title: "处理问题",
	   			closeAction: 'hide',
				resizable:false,
				modal:true,
				width:400,
				height:200,
				items :[{
					xtype:'form'
					,layout:'form'
					,border:false
					,labelAlign:"left"
					,frame:true
					,items:[{
						fieldLabel:'处理描述'
						,xtype:'textarea'
						,labelWidth:65
						,name:'tsCdrCountTotal.dealText'
						,height:100
						,anchor:'95%'
					}]
					,minButtonWidth:50
			   		,buttonAlign:'center'
				   	,buttons:[{
				   		text:'提交'
				   		,handler:function(btn) {
				   			var form = btn.ownerCt.ownerCt.getForm();
				   				mPanel.execInfo(mPanel,form);
				   			} 
				   		}]
				}]
				
   			});
   		}
   	    mPanel.updatePanel.show();
   	},	execInfo: function(m,form) {
   		var gp = m.items.get(1) ; 	
   		var rows= gp.getSelectionModel().getSelections(); 
   		var ids = "";
   		var types = "";
   			
   		for(var i=0 ;i<rows.length;i++) {
   			if(i==0) {
   				ids = rows[i].data['seqId'] ;
   			}else{
   				ids = ids +","+rows[i].data['seqId'] ;
   			}
   		}
   		form.submit( {	
				waitTitle : '请稍候...',
				waitMsg : '正在提交表单数据,请稍候...',
				method : 'post',
				params : {
					ids:ids, 
					types:this.types=1
				},
				
				url : 'cdr_updateCdrFlag.action',
				scope : this,
				success : function(form, action) {
					var Result=action.result;
					if(Result.success) {
						gp.getStore().reload();
						m.updatePanel.hide() ;
						MsgTip.msg({
						 	msg:"处理成功~~"
						 	,cmp: m
						 	,pos:"top"
						 	,time:2
						});
					} else {
						m.updatePanel.hide() ;
						MsgTip.msg({
						 	msg:"处理异常,请重试~"
						 	,cmp: m
						 	,pos:"top"
						 	,time:2
						});
					}
				},
				failure : function() {
						m.updatePanel.hide() ;
						MsgTip.msg({
						 	msg:"网络异常,请重试~"
						 	,cmp: m
						 	,pos:"top"
						 	,time:2
						});
					}
				});
   	}
   	
	,exportData:function(btn){
		var gp = btn.ownerCt.ownerCt.items.get(1) ;
			var baseParams = gp.getStore().baseParams;
   		var ul = Ext.urlEncode(baseParams);
   		var st = gp.getStore();
   		Ext.MessageBox.confirm("确认", "确定导出?", function(btn) {
			if (btn == "yes") {
				window.location.href ="cdr_exportCdrCountTotalToExcel.action?"+CDR.countTotal.conditions+"&start=0&limit="+st.totalLength;
			}
		});
   	} 
});

CDR.countTotal.conditions = "";

CDR.countTotal.gridPanel = Ext.extend(Ext.grid.GridPanel,{
	header: false,
	loadMask:{msg:'加载中...'},
	enableDragDrop: false,
	height: 560,
	columnLines:true,
	allowHeaderWrap:true,
	allowWrap:false,
	initComponent : function(){
		var thisGrid = this;
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
//					dataIndex: "ID",
	                sortable: false,
	                singleSelect:false,
	                width:20
				});
   	 	this.recordStart = 1 ;
		var rn = new　Ext.grid.RowNumberer({
		　　header　:　"序号",
		　　width　:　35,
		　　renderer:function(value,metadata,record,rowIndex){
		　　　return　　thisGrid.recordStart　+　rowIndex;
		　　}
		});
   	    var cdrCountTotalStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'cdr_getCdrCountTotal.action'
        	 ,fields:[ 'taskSeq','seqId','startDate','eparchyCode','num01',
        		 'num02','num03','num07','num08','num11','num12','num13',
        		 'num14','cityCode','dbNo','dealTime','dealFlag','dealText','dealTime']
        	 ,root:'root'
        });
   	    this.store = cdrCountTotalStore;
		this.sm = selectMode;
		this.bbar = [new Ext.PagingToolbar({
					pageSize : 200,
					store : cdrCountTotalStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							thisGrid.recordStart = obj.start+1 ;  
						}
					}
				})];
		this.columns = [selectMode
					,rn
					,{header:'详单日期',
						sortable:true,
						dataIndex:"startDate"
						,headerAlign :"center"
						,align :"left"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:80
					},{
				        header:'归属地市',
						sortable:true
						,dataIndex:"eparchyCode"
						,width:60
						,headerAlign :"center"
						,align :"left"
						,allowHeaderWrap:true
						,allowWrap:false
					},{
				        header:'移网语音',
						sortable:true
						,dataIndex:"num01"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
				        header:'移网短信',
						sortable:true
						,dataIndex:"num02"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
				        header:'移网gprs',
						sortable:true
						,dataIndex:"num03"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
				        header:'移网ip',
						sortable:true
						,dataIndex:"num07"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
						header:'移网sp',
						sortable:true
						,dataIndex:"num08"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
						header:'固网语音',
						sortable:true,
						dataIndex:"num11"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
						header:'固网短信',
						sortable:true,
						dataIndex:"num12"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
						header:'固网宽带',
						sortable:true,
						dataIndex:"num13"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					},{
						header:'固网代收',
						sortable:true,
						dataIndex:"num14"
						,headerAlign :"center"
						,align :"right"
						,allowHeaderWrap:true
						,allowWrap:false
						,width:100
					}
						,{
						header:'是否处理',
						sortable:true,
						headerAlign:"center",
						dataIndex:"dealFlag",
						renderer:function(v) {
							return v==1?"已处理":"未处理";
						}
					},{
						header:'处理描述',
						sortable:true,
						headerAlign:"center",
						dataIndex:"dealText",
						cellTip:true,
						renderer:function(value, metaData, record, rowIndex, colIndex, store) {
			   			metaData.attr = ' ext:qtip="' + value + '"';
			   			return value ;
			   			}
					}
			];
		cdrCountTotalStore.load({
			params:{
				start:0,
				limit:200
			}
		});
		CDR.countTotal.gridPanel.superclass.initComponent.call(this);
	}
});

CDR.countTotal.formPanel = Ext.extend(Ext.form.FormPanel,{
		text:"表单",
		layout:"form",
		region : "center",
		labelAlign:"right",
		width:550,
		height:300,
		frame:true,
		initComponent:function(){
				var cityNameSelect = new Ext.data.JsonStore({
				    url: "highFee_getCityCode.action?type=1",//第一个comboBox的json数据的URL地址
				    root: "root",
				    fields: ["paramValue", "displayValue"]
				});
				this.items=[
							{fieldLabel : '详单日期',
								name : 'statDay',
								xtype:"datefield",
								editable : true,
								format:'Y-m-d',
								emptyText:"请选择",
								anchor:"90%"
								,width:60
					    	}
								,{fieldLabel : '归属地市',
								name : 'eparchyCode',
								xtype : 'combo',
								editable : false,
								mode : 'remote',
								triggerAction : 'all',
								valueField: "paramValue",
	   							displayField: "displayValue",
								store : areaCodeSelect,
								emptyText:"请选择",
								anchor:"90%"
								,width:60
								,listeners : {
			 						"select": function(combo){
										combo.ownerCt.getForm().findField("cityCode").reset();
										cityNameSelect.baseParams={'areaCode':combo.getValue()};
										cityNameSelect.load();
		 							}
								}
							}
							,{fieldLabel : '归属区县',
								name : 'cityCode',
								xtype : 'combo',
								editable : false,
								mode : 'remote',
								triggerAction : 'all',
								valueField: "paramValue",
	   							displayField: "displayValue",
								store : cityNameSelect,
								emptyText:"请选择",
								anchor:"90%"
								,width:70
							}
							
//							,{fieldLabel : '稽核日期',
//								name : 'dealTime',
//								xtype:"datefield",
//								editable : true,
//								format:'Y-m-d',
//								emptyText:"请选择",
//								anchor:"90%"
//					    	}
			];
			CDR.countTotal.formPanel.superclass.initComponent.call(this);
		}
});
/**
 * 查询面板
 */
CDR.countTotal.searchPanel = Ext.extend( Ext.Window,{
   		closeAction: 'hide',
		resizable:false,
		modal:true,
		width:550,
		height:350
   		,layout:"fit"
   		,title: "按条件查询差异话单"
   		,initComponent:function(){
			this.items = [new CDR.countTotal.formPanel()];
			CDR.countTotal.searchPanel.superclass.initComponent.call(this);
   		}
   		,buttonAlign :'center'
		,minButtonWidth:60
		,buttons:[{
			text:"确定",
			handler : function(btn){
				var a = btn.ownerCt.ownerCt.items.get(0);
			    var thisForm = a.getForm();
			    var eparchyCode  = thisForm.findField('eparchyCode').getValue();
			    var cityCode = thisForm.findField('cityCode').getValue();
			    var statDay = thisForm.findField('statDay').getValue();
			    	statDay = statDay?statDay.format("Ymd"):"";
			    var dealTime = thisForm.findField('dealTime').getValue();
			    var store = btn.ownerCt.ownerCt.thisGrid.getStore();
			    
			    //保存条件
			    CDR.countTotal.conditions =
			    	'tsCdrCountTotal.eparchyCode='+eparchyCode+
					'&tsCdrCountTotal.cityCode='+cityCode+
					'&tsCdrCountTotal.startDate='+statDay+
					'&tsCdrCountTotal.dealTime='+dealTime;
			    
			    btn.ownerCt.ownerCt.thisGrid.recordStart = 1;
			    store.baseParams = {
					'tsCdrCountTotal.eparchyCode':eparchyCode
					,'tsCdrCountTotal.cityCode':cityCode
					,'tsCdrCountTotal.startDate':statDay
					,'tsCdrCountTotal.dealTime':dealTime
				};
	
	            store.load({params:{start:0, limit:200}});
			    
			   btn.ownerCt.ownerCt.hide();//隐藏弹出页面
			}
		}, {
			text : '重置',
			handler : function() {this.ownerCt.ownerCt.form.reset();}
		}, {
			text : '取消',
			handler : function() {this.ownerCt.ownerCt.hide();}
		}]
});
//条件查询隐藏出现
QueryStatcdrCounTotalForm = Ext.extend(Ext.form.FormPanel,{
	text:"表单",
	layout:"form",
	region : "center",
	labelAlign:"right",
	collapsible:true,
	collapsed:false,
	header:false,
	width:"100%",
	height:1,
	frame:true,
	layout:'table',
	layoutConfig:{
	tableAttrs: {
		align:'left'
	 },
	columns: 5
 	},
	initComponent: function(){
	var cityNameSelect = new Ext.data.JsonStore({
	    url: "highFee_getCityCode.action?type=1",//第一个comboBox的json数据的URL地址
	    root: "root",
	    fields: ["paramValue", "displayValue"]
	});
	this.items=[{ 
		    	layout : "form",
		    	xtype:'panel',
		    	colspan:1,
		    	items:[{fieldLabel : '归属地市',
							name : 'eparchyCode',
							xtype : 'combo',
							editable : false,
							mode : 'remote',
							triggerAction : 'all',
							valueField: "paramValue",
   							displayField: "displayValue",
							store : areaCodeSelect,
							emptyText:"请选择",
							width:100,
							listeners : {
		 						"select": function(combo){
									combo.ownerCt.ownerCt.getForm().findField("cityCode").reset();
									cityNameSelect.baseParams={'areaCode':combo.getValue()};
									cityNameSelect.load();
		 						}
							}
						}]},{ 
		    	layout : "form",
		    	xtype:'panel',
		    	colspan:1,
		    	items:[{fieldLabel : '归属区县',
							name : 'cityCode',
							xtype : 'combo',
							editable : false,
							mode : 'remote',
							triggerAction : 'all',
							valueField: "paramValue",
   							displayField: "displayValue",
							store : cityNameSelect,
							emptyText:"请选择",
							width:100
						}]}
               
				,{layout : "form",
		    	xtype:'panel',
		    	colspan:1,
		    	items:[{fieldLabel : '详单日期',
								name : 'statDay',
								xtype:"datefield",
								editable : true,
								format:'Y-m-d',
								emptyText:"请选择",
								width:100
					    	}]}
//			,{layout : "form",
//		    	xtype:'panel',
//		    	colspan:1,
//		    	items:[{fieldLabel : '稽核日期',
//								name : 'dealTime',
//								xtype:"datefield",
//								editable : true,
//								format:'Y-m-d',
//								emptyText:"请选择",
//								width:100
//					    	}]}
//			
		];
	  QueryStatcdrCounTotalForm.superclass.initComponent.call(this);
	},
	buttonAlign :'center',
	minButtonWidth:60,
	buttons:[{
	text:"确定",
		handler : function(btn){
				
		   		var thisForm = btn.ownerCt.ownerCt.getForm();
		  		var thisForm = btn.ownerCt.ownerCt.getForm();
			     var eparchyCode  = thisForm.findField('eparchyCode').getValue();
			    var cityCode = thisForm.findField('cityCode').getValue();
			    var statDay = thisForm.findField('statDay').getValue();
			    	statDay = statDay?statDay.format("Ymd"):"";
			    var dealTime = thisForm.findField('dealTime').getValue();
			    var store = btn.ownerCt.ownerCt.thisGrid.getStore();
			    
			    //保存条件
			    CDR.countTotal.conditions =
			    	'tsCdrCountTotal.eparchyCode='+eparchyCode+
					'&tsCdrCountTotal.cityCode='+cityCode+
					'&tsCdrCountTotal.startDate='+statDay+
					'&tsCdrCountTotal.dealTime='+dealTime;
			    
			    btn.ownerCt.ownerCt.thisGrid.recordStart = 1;
			    store.baseParams = {
					'tsCdrCountTotal.eparchyCode':eparchyCode
					,'tsCdrCountTotal.cityCode':cityCode
					,'tsCdrCountTotal.startDate':statDay
					,'tsCdrCountTotal.dealTime':dealTime
				};

            store.load({params:{start:0, limit:200}});
		    
		   //this.ownerCt.ownerCt.ownerCt.hide();//隐藏弹出页面
		}
	}, {
		text : '重置',
		handler : function() {this.ownerCt.ownerCt.form.reset();}
	}, {
		text : '关闭',
		handler : function() {
		this.ownerCt.ownerCt.collapse(true);
			var m = this.ownerCt.ownerCt.ownerCt;
                                   	var g = m.items.get(1);
                                   	var a = m.items.get(0);
			g.setHeight(m.getHeight()-m.tbar.dom.clientHeight-1);
   			this.ownerCt.ownerCt.searchBtn.setText("显示查询");
   			this.ownerCt.ownerCt.searchBtn.setIconClass("ico-dot-search-open");
		}
	}]
});